// pages/course/jingjiangCourse/jingjiangCourse.js
import Notify from '@vant/weapp/notify/notify';
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ['1'],
    is:true,
    // 判断是否购买
    buy:false,
    //用户id
    id:1,
    //课程id
    kcid:8,
    //图片
    url:'http://localhost:80/file/downLoad?fileName=',
    img:'',
    //视频
    src:'',
    //视频总时长
    srcdate:0,
    //课程名
    name:'',
    //课程简介
    courseIntroduce:'',
    //适用人群
    applicablePopulation:'',
    //学习收获
    learnHarvest:'',
    //订阅需知
    subscriptionNotice:'',
    //原始价格
    originalPrice:0,
    //现在价格
    discountPrice:0,
    //学习总数
    learningTimes:0,
    //评价数量
    pjCount:0,
    //收藏数量
    collects:0,
    //课程有效期
    termOfValidity:'',
    //咨询师id
    typeId:0,
    //咨询师信息------------------------------------------
    //咨询师姓名
    zxsname:'',
    //性别
    zxssex:'',
    //咨询师地址
    zxsaddress:'',
    // 咨询师头像
    zxsimg:'',
    //是否认证
    zxsauthentication:0,
    //课堂交流
    coursemark:[],
    //是否收藏
    collect:true,
    //课程对象负责修改课程有关
    course:{
      //课程id
      id:0,
      //课程评价数量
      pjCount:0,
    },
    //添加和取消评论
    comment:true,
    //评论内容
    commenttext:'',
    //用户信息
    userInfor:'',
    //购买对象
    CourseBuy:{
      //课程id
      courseId:0,
      //用户id
      memberId:0,
      //购买价格
      buytime:0.0,
      //购买时间
      buytime:'',
      buyState:2,
    },
    //评论内容
    value:'',
    //评分
    value1:0,
    // 评价请求内容对象
    coursemark1:{
      content:'',
      grade:0,
      userId:0,
      courseId:0,
    },
   
   
  },
  onChangeaaa(event) {
    let a=this;
    a.setData({
      value1: event.detail,
    });
    console.log(a.data.value1);
  },
  onChangebbb(event) {
    let a=this;
    a.setData({
      value: event.detail,
    });
    console.log(a.data.value);
  },

//添加评论
tianjiacomment(){
  let a=this;
  a.setData({
    comment:false,
  })
},
//提交评论
tianjiacommenta(){
  let a=this;
  let b=getApp();
  a.setData({
    'coursemark1.content':a.data.value,
    'coursemark1.grade':a.data.value1,
    'coursemark1.userId':a.data.userInfor.id,
    'coursemark1.courseId':a.data.kcid
  })
wx.request({
  url:b.globalData.URL+ 'coursemark/addCoursemark',
  method:"POST",
  data: JSON.stringify(a.data.coursemark1),
  header: {
     'content-type': 'application/json'
  },
  success(res){
    wx.showToast({
      title: '评论成功', // 提示内容
      icon: 'success', // 图标，可选 'success'、'loading'、'none' 等
      duration: 2000 // 提示持续时间（毫秒），默认 1500
    });
    a.jiaolou();
  }
})

},
//取消评论
qxcomment(){
  let a=this;
  a.setData({
    comment:true,
  })
},
//获取评论内容
onChange(event) {
  // event.detail 为当前输入的值
  console.log(event.detail);
},

  //购买课程
  onClickButton(){
    let a=this;
    let b=getApp();
    console.log(a.data.userInfor);
    if(a.data.userInfor==''){
        wx.showModal({
          title: '没有登录无法购买',
          content: '确认登录后则登录',
          success: function (res) {
          if (res.confirm) {
            //跳转到登录页面
            wx.navigateTo({
              url: '/pages/mine/login/login',
            })
          } else {
          console.log('用户点击取消');
              }
          }
          });
    }else{
          if(!a.data.buy){
            wx.showModal({
              title: '是否购买课程',
              content: '确认后将购买课程，不购买将无法观看视频',
              success: function (res) {
              if (res.confirm) {
                  a.setData({
                    'CourseBuy.courseId':a.data.kcid,
                    // 'CourseBuy.memberId':1,
                     'CourseBuy.memberId':a.data.userInfor.id,
                    'CourseBuy.buytime' :a.data.discountPrice,
                  }),
                //用户购买课程
                wx.request({
                  url:b.globalData.URL+'CourseBuyController/insertCourseBuy',
                  method:"POST",
                  data: JSON.stringify(a.data.CourseBuy),
                    header: {
                       'content-type': 'application/json'
                   },
                  success(res){
                    if (res.data.code==1){
                      a.setData({
                        buy:true
                      })
                      Notify({
                        type: 'success',
                        message: '购买成功！',
                        duration: 1000,
                        selector: '#van-notify',
                      })
                    }else{
                      Notify({
                        type: 'success',
                        message: '购买失败！',
                        duration: 1000,
                        selector: '#van-notify',
                      })
                    }
                  }
                })
              } else {
              console.log('用户点击取消');
                  }
              }
              });
          }
    }
     
  },
  // 获取视频时长
getLength(e) {
  e.detail.duration = parseInt(e.detail.duration)
  //处理时间格式并存放到data中
  this.setData({
      srcdate:  parseInt(e.detail.duration / 60) + ":" + e.detail.duration % 60
  })
},
  // 获取课堂评价
  jiaolou(){
    let a=this;
    let b =getApp();
    wx.request({
      url:b.globalData.URL+'coursemark/findCoursemarkByCourseId?id='+this.data.kcid,
      method:"GET",
      success(res){
        console.log(res)
        //评价数量
        let c=res.data.data.length;
        a.setData({
          coursemark:res.data.data,
          pjCount:c,
          'course.pjCount':c,
          'course.id':a.data.kcid
        })
        //课程评价数量添加
        wx.request({
          url:b.globalData.URL+ 'course/updatecourse?course='+a.data.course,
          method:"PUT"
        })
      }
    })
  },

  //是否收藏
  collect(){
    let a=this;
    let b=getApp();
    if(a.data.userInfor!=''){
    if(!a.data.collect){
     console.log("取消收藏");
     wx.request({
       url:b.globalData.URL+ 'coursecollection/deleteCoursecollection?userid='+b.globalData.userInfor.id+'&kcid='+a.data.kcid,
       method:'GET',
       success(res){
         console.log(res);
         if(res.data.code==1){
          a.setData({
            collect:true
          })
         }

       }
     })
     //添加收藏
    }else{
      if(b.globalData.userInfor!=''){
      a.setData({
        collect:false
      })
      console.log("添加");
      //添加收藏
        wx.request({
          url:b.globalData.URL+'coursecollection/insertCoursecollection?userid='+b.globalData.userInfor.id+'&kcid='+a.data.kcid,
          method:"GET",
          success(res){
            console.log('========',res);
            if(res.data.code==1){
              a.setData({
                collect:false
              })
            }
          }
        })

    }
    }
  }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let a=this;
    let b =getApp();
      //课程id
      const id = options.id;
      console.log('接收到的 id 值为:', id);
      this.setData({
        kcid: id
      });
   
    console.log("用户信息",b.globalData.userInfor);
    if(b.globalData.userInfor!=''){
        //获取用户信息
    a.setData({
      userInfor:b.globalData.userInfor,
      // id:b.globalData.userInfor.id
    });
    wx.request({
      url:b.globalData.URL+'coursecollection/selectCoursemarkByUserIdAndKcid?userId='+a.data.userInfor.id+'&kcid='+a.data.kcid,
      method:'GET',
      success(res){
        console.log('用户是否收藏？',res);
        if(res.data.code== '-1'){
        a.setData({
           collect:true
          })
        }else{
          a.setData({
            collect:false
          })
        }
      }
    })
    }
//根据id获取课程
    wx.request({
      url:b.globalData.URL+ 'course/findcoursebycourseid?id='+a.data.kcid,
      method:"PUT",
      success(res){
        console.log(res);
        let c=res.data.data
        a.setData({
          img:a.data.url +c.tabularChart,
          src:a.data.url+c.courseVideo,
          name:c.courseName,
          courseIntroduce:c.courseIntroduce,
          applicablePopulation:c.applicablePopulation,
          learnHarvest:c.learnHarvest,
          subscriptionNotice:c.subscriptionNotice,
          originalPrice:c.originalPrice,
          discountPrice:c.discountPrice,
          learningTimes:c.learningTimes,
          pjCount:c.pjCount,
          collects:c.collects,
          termOfValidity:c.termOfValidity,
          typeId:c.typeId,
          
        })
        wx.request({
          url: b.globalData.URL+ 'consult/'+a.data.typeId,
          method:'GET',
          success(ress){
            let d=ress.data.data;
            a.setData({
              zxsname:d.name,
              zxssez:d.sex,
              zxsaddress:d.address,
              zxsimg:a.data.url+d.img,
              zxsauthentication:d.authentication,
            })
          }
        })
      },
      fail(err){
        console.log(err);
      }
    });
     //判断用户是否购买
     if(b.globalData.userInfor!=''){
      wx.request({
        url:b.globalData.URL+ 'CourseBuyController/selectCourseBuyByCourseId?courseId='+a.data.kcid+'&memberId='+a.data.id,
        method:"GET",
        success(res){
          console.log("用户是否购买",res);
          if(res.data.code==1){
            a.setData({
              buy:true
            })
          }
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  onShow(){
    this.setData({
      is: !this.data.is
    });
  }
})